package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;
import static com.migu.contacts.util.DialogUtil.showVIPDialog;

import android.util.Log;
import android.view.View;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.BottomPopup;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.GroupBean;
import com.migu.contacts.databinding.ActivityGroupEditBinding;
import com.migu.contacts.databinding.GroupAddBinding;
import com.migu.contacts.databinding.ItemGroupEditBinding;
import com.migu.contacts.databinding.PopGroupEditBinding;
import com.migu.contacts.util.UserCenter;

import java.util.List;

public class GroupEditActivity extends BaseActivity<ActivityGroupEditBinding> {

    @BindAdapter(R.id.recyclerView)
    BaseAdapter<ItemGroupEditBinding, GroupBean> adapter = new BaseAdapter<ItemGroupEditBinding, GroupBean>() {
        @OnClick(R.id.edit)
        public void edit(GroupBean bean) {
            showPop(bean);
        }

        @Override
        protected void onBindData(ItemGroupEditBinding binding, GroupBean bean, int position) {
            super.onBindData(binding, bean, position);
            if (position == mList.size() - 1) {
                binding.space.setVisibility(View.VISIBLE);
            } else {
                binding.space.setVisibility(View.GONE);
            }
        }
    };

    private void showPop(GroupBean bean) {
        BottomPopup bottomPopup = new BottomPopup<PopGroupEditBinding>(this) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @Override
            protected void initData() {
                super.initData();
                mBinding.input.setText(bean.name);
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                bean.name = mBinding.input.getText().toString().trim();
                API.editGroup(bean).watch(mActivity, new HttpCallback<Boolean>() {
                    @Override
                    protected void onSuccess(Boolean bean) {
                        adapter.notifyDataSetChanged();
                        dismiss();
                    }
                });
                dismiss();
            }
        };
        new XPopup.Builder(this).asCustom(bottomPopup).show();

    }

    @Override
    protected void initData() {
        super.initData();
        API.queryGroup().watch(this, new HttpCallback<List<GroupBean>>() {
            @Override
            protected void onSuccess(List<GroupBean> groupBeans) {
                adapter.setData(groupBeans);
            }
        });
    }

    public void add(View view) {
        if (adapter.getData().size() >= 5 && !UserCenter.isVip()) {
            showVIPDialog(mActivity);
            return;
        }
        BottomPopup bottomPopup = new BottomPopup<GroupAddBinding>(mActivity) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @Override
            protected void initData() {
                super.initData();
                mBinding.setBean(new GroupBean());
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                API.addGroup(mBinding.getBean()).watch(mActivity, new HttpCallback<GroupBean>() {
                    @Override
                    protected void onSuccess(GroupBean bean) {
                        Log.d(TAG, "onSuccess() called with: bean = [" + bean + "]");
                        adapter.getData().add(bean);
                        adapter.notifyDataSetChanged();
                        dismiss();
                    }
                });
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }
}